﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dbPoll_Application.ViewModels;
using dbPoll_Application.Models;
using System.Web.Routing;
using dbPoll_Application.Helpers;

namespace dbPoll_Application.Controllers
{
    public class QuestionController : Controller
    {
        [Authorize]
        public ActionResult EditCustom(int questionID, int questionNumber, int pollID, String pollName, Boolean? saved)
        {
            var poll = PollRepository.GetPollByID(pollID);
            ViewData["testPoll"] = poll.isTestPoll.ToString();
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            var question = QuestionRepository.GetQuestionByID<CustomQuestion>(questionID);
            if (!SecurityHelper.CanEditQuestion(user, poll, question))
            {
                return RedirectToAction("Index", "Home");
            }
            if (saved.HasValue)
            {
                ViewData["saved"] = "true";
            }
            else
            {
                ViewData["saved"] = "false";
            }
            ViewData["templates"] = TemplateRepository.GetAllOptionsTemplateNames();
            ViewData["compareWith"] = "Search for a question...";
            ViewData["pollID"] = pollID;
            ViewData["pollName"] = pollName;
            ViewData["pollCLose"] = poll.pollClose;
            ViewData["pollStart"] = poll.pollStart;
            ViewData["pollRoles"] = SecurityHelper.GetRolesForPole(user, poll);
            if (questionID < 0)
            {
                return View(new CustomQuestion(questionNumber, poll.defaultFontType, poll.defaultFontColour, poll.defaultFontSize, poll.defaultResultsChart, poll.defaultCountdownTimer, poll.defaultResponseCounter, poll.defaultCorrectAnswerIndicator, poll.defaultResponseGrid));
            }
            if (question.compareWith != null)
            {
                poll = PollRepository.GetPollContainingQuestion(question.compareWith);
                ViewData["compareWith"] = poll.name + " > " + question.question;
            }
            ViewData["hasCompare"] = QuestionRepository.HasCompared(questionID);
            return View(question);
        }

        [Authorize]
        public ActionResult EditRanking(int questionID, int questionNumber, int pollID, String pollName, Boolean? saved)
        {
            var poll = PollRepository.GetPollByID(pollID);
            ViewData["testPoll"] = poll.isTestPoll.ToString();
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            var question = QuestionRepository.GetQuestionByID<RankingQuestion>(questionID);
            if (!SecurityHelper.CanEditQuestion(user, poll, question))
            {
                return RedirectToAction("Index", "Home");
            }
            if (saved.HasValue)
            {
                ViewData["saved"] = "true";
            }
            else
            {
                ViewData["saved"] = "false";
            }
            ViewData["compareWith"] = "Search for a question...";
            ViewData["pollID"] = pollID;
            ViewData["pollCLose"] = poll.pollClose;
            ViewData["pollStart"] = poll.pollStart; 
            ViewData["pollName"] = pollName;
            ViewData["pollRoles"] = SecurityHelper.GetRolesForPole(user, poll);
            if (questionID < 0)
            {
                return View(new RankingQuestion(questionNumber, poll.defaultFontType, poll.defaultFontColour, poll.defaultFontSize, poll.defaultResultsChart, poll.defaultCountdownTimer, poll.defaultResponseCounter, poll.defaultCorrectAnswerIndicator, poll.defaultResponseGrid));
            }
            if (question.compareWith != null)
            {
                poll = PollRepository.GetPollContainingQuestion(question.compareWith);
                ViewData["compareWith"] = poll.name + " > " + question.question;
            }
            return View(question);
        }

        [Authorize]
        public ActionResult EditDemographic(int questionID, int questionNumber, int pollID, String pollName, Boolean? saved)
        {
            var poll = PollRepository.GetPollByID(pollID);
            ViewData["testPoll"] = poll.isTestPoll.ToString();
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            var question = QuestionRepository.GetQuestionByID<DemographicQuestion>(questionID);
            if (!SecurityHelper.CanEditQuestion(user, poll, question))
            {
                return RedirectToAction("Index", "Home");
            }
            if (saved.HasValue)
            {
                ViewData["saved"] = "true";
            }
            else
            {
                ViewData["saved"] = "false";
            }
            ViewData["compareWith"] = "Search for a question...";
            var participantFields = new List<ParticipantField>();
            participantFields.AddRange(poll.participantFields);
            ViewData["participantFields"] = participantFields;
            ViewData["pollID"] = pollID;
            ViewData["pollName"] = pollName;
            ViewData["pollCLose"] = poll.pollClose;
            ViewData["pollStart"] = poll.pollStart;
            ViewData["pollRoles"] = SecurityHelper.GetRolesForPole(user, poll);
            if (questionID < 0)
            {
                return View(new DemographicQuestion(questionNumber, poll.defaultFontType, poll.defaultFontColour, poll.defaultFontSize, poll.defaultResultsChart, poll.defaultCountdownTimer, poll.defaultResponseCounter, poll.defaultCorrectAnswerIndicator, poll.defaultResponseGrid));
            }
            if (question.compareWith != null)
            {
                poll = PollRepository.GetPollContainingQuestion(question.compareWith);
                ViewData["compareWith"] = poll.name + " > " + question.question;
            }
            ViewData["hasCompare"] = QuestionRepository.HasCompared(questionID);
            return View(question);
        }

        [Authorize]
        public ActionResult EditNumeric(int questionID, int questionNumber, int pollID, String pollName)
        {
            var poll = PollRepository.GetPollByID(pollID);
            ViewData["testPoll"] = poll.isTestPoll.ToString();
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            var question = QuestionRepository.GetQuestionByID<NumericQuestion>(questionID);
            if (!SecurityHelper.CanEditQuestion(user, poll, question))
            {
                return RedirectToAction("Index", "Home");
            }
            ViewData["pollID"] = pollID;
            ViewData["pollName"] = pollName;
            ViewData["pollStart"] = poll.pollStart;
            ViewData["pollClose"] = poll.pollClose;
            ViewData["pollRoles"] = SecurityHelper.GetRolesForPole(user, poll);
            if (questionID < 0)
            {
                return View(new NumericQuestion(questionNumber, poll.defaultFontType, poll.defaultFontColour, poll.defaultFontSize, poll.defaultResultsChart, poll.defaultCountdownTimer, poll.defaultResponseCounter, poll.defaultCorrectAnswerIndicator, poll.defaultResponseGrid));
            }
            return View(question);
        }

        [Authorize]
        public ActionResult EditAlphanumeric(int questionID, int questionNumber, int pollID, String pollName, Boolean? saved)
        {
            var poll = PollRepository.GetPollByID(pollID);
            ViewData["testPoll"] = poll.isTestPoll.ToString();
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            var question = QuestionRepository.GetQuestionByID<AlphanumericQuestion>(questionID);
            if (!SecurityHelper.CanEditQuestion(user, poll, question))
            {
                return RedirectToAction("Index", "Home");
            }
            if (saved.HasValue)
            {
                ViewData["saved"] = "true";
            }
            else
            {
                ViewData["saved"] = "false";
            }
            ViewData["pollID"] = pollID;
            ViewData["pollName"] = pollName;
            ViewData["pollStart"] = poll.pollStart;
            ViewData["pollRoles"] = SecurityHelper.GetRolesForPole(user, poll);
            if (questionID < 0)
            {
                return View(new AlphanumericQuestion(questionNumber, poll.defaultFontType, poll.defaultFontColour, poll.defaultFontSize, poll.defaultResultsChart, poll.defaultCountdownTimer, poll.defaultResponseCounter, poll.defaultCorrectAnswerIndicator, poll.defaultResponseGrid));
            }
            return View(question);
        }

        private void PopulateQuestion(Question q1, Question q2)
        {
            q1.question = q2.question;
            q1.questionNumber = q2.questionNumber;
            q1.fontType = q2.fontType;
            q1.fontColour = q2.fontColour;
            q1.fontSize = q2.fontSize;
            q1.countdownTimer = q2.countdownTimer;
            q1.responseCounter = q2.responseCounter;
            q1.correctAnswerIndicator = q2.correctAnswerIndicator;
            q1.responseGrid = q2.responseGrid;
            q1.resultsChart = q2.resultsChart;
            if (q2.comparisonChart != null)
            {
                q1.comparisonChart = q2.comparisonChart;
            }
            q1.asynchronous = q2.asynchronous;
        }

        private void PopulateMultipleChoiceQuestion(MultipleChoiceQuestion q1, MultipleChoiceQuestion q2)
        {
            q1.allowedAnswers = q2.allowedAnswers;
            for (int i = 0; i < q1.options.Count; i++)
            {
                q1.options[i].answer = q2.options[i].answer;
                q1.options[i].correctCurrent = q2.options[i].correctCurrent;
                q1.options[i].correctOriginal = q2.options[i].correctOriginal;
                q1.options[i].pointsCurrent = q2.options[i].pointsCurrent;
                q1.options[i].pointsOriginal = q2.options[i].pointsOriginal;
            }
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public int AddCustom(CustomQuestion q, int pollID)
        {
            var question = new CustomQuestion(q.questionNumber);
            var poll = PollRepository.GetPollByID(pollID);
            PopulateQuestion(question, q);
            PopulateMultipleChoiceQuestion(question, q);
            question.compareWith = QuestionRepository.GetQuestionByID<CustomQuestion>(q.compareWith.questionID);
            poll.questions.Add(question);
            PollRepository.UpdatePoll(poll);

            return question.questionID;
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public int AddRanking(RankingQuestion q, int pollID)
        {
            var question = new RankingQuestion(q.questionNumber);
            var poll = PollRepository.GetPollByID(pollID);
            PopulateQuestion(question, q);
            PopulateMultipleChoiceQuestion(question, q);
            question.compareWith = QuestionRepository.GetQuestionByID<RankingQuestion>(q.compareWith.questionID);
            question.rankValues = q.rankValues;
            poll.questions.Add(question);
            PollRepository.UpdatePoll(poll);

            return question.questionID;
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public int AddDemographic(DemographicQuestion q, int pollID)
        {
            var question = new DemographicQuestion(q.questionNumber);
            var poll = PollRepository.GetPollByID(pollID);
            PopulateQuestion(question, q);
            PopulateMultipleChoiceQuestion(question, q);
            question.compareWith = QuestionRepository.GetQuestionByID<DemographicQuestion>(q.compareWith.questionID);
            question.linkedField = ParticipantRepository.GetParticipantFieldByID(q.linkedField.fieldID);
            poll.questions.Add(question);
            PollRepository.UpdatePoll(poll);

            return question.questionID;
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public int AddNumeric(NumericQuestion q, int pollID)
        {
            var question = new NumericQuestion(q.questionNumber);
            var poll = PollRepository.GetPollByID(pollID);
            PopulateQuestion(question, q);
            question.greaterThan = q.greaterThan;
            question.lessThan = q.lessThan;
            poll.questions.Add(question);
            PollRepository.UpdatePoll(poll);

            return question.questionID;
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public int AddAlphanumeric(AlphanumericQuestion q, int pollID)
        {
            var question = new AlphanumericQuestion(q.questionNumber);
            var poll = PollRepository.GetPollByID(pollID);
            PopulateQuestion(question, q);
            question.answers = q.answers;
            poll.questions.Add(question);
            PollRepository.UpdatePoll(poll);

            return question.questionID;
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public Boolean UpdateCustom(CustomQuestion q)
        {
            var question = QuestionRepository.GetQuestionByID<CustomQuestion>(q.questionID);
            PopulateQuestion(question, q);
            PopulateMultipleChoiceQuestion(question, q);
            question.compareWith = QuestionRepository.GetQuestionByID<CustomQuestion>(q.compareWith.questionID);

            return QuestionRepository.UpdateQuestion<CustomQuestion>(question);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public Boolean UpdateRanking(RankingQuestion q)
        {
            var question = QuestionRepository.GetQuestionByID<RankingQuestion>(q.questionID);
            PopulateQuestion(question, q);
            PopulateMultipleChoiceQuestion(question, q);
            question.compareWith = QuestionRepository.GetQuestionByID<RankingQuestion>(q.compareWith.questionID);
            question.rankValues = q.rankValues;

            return QuestionRepository.UpdateQuestion<RankingQuestion>(question);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public Boolean UpdateDemographic(DemographicQuestion q)
        {
            var question = QuestionRepository.GetQuestionByID<DemographicQuestion>(q.questionID);
            PopulateQuestion(question, q);
            PopulateMultipleChoiceQuestion(question, q);
            question.compareWith = QuestionRepository.GetQuestionByID<DemographicQuestion>(q.compareWith.questionID);
            question.linkedField = ParticipantRepository.GetParticipantFieldByID(q.linkedField.fieldID);
            ParticipantField field = question.linkedField;
            if (field != null)
            {
                IList<QuestionOption> options = question.options.ToList();
                foreach (QuestionOption option in options)
                {
                    foreach (OptionResponse response in option.responses)
                    {
                        ParticipantFieldValue fieldValue = null;
                        fieldValue = field.fieldValues.Where(m => m.participant == response.participant).FirstOrDefault<ParticipantFieldValue>();
                        if (fieldValue == null)
                        {
                            fieldValue = new ParticipantFieldValue(response.participant, option.answer);
                            field.fieldValues.Add(fieldValue);
                        }
                        else if (!fieldValue.value.Equals(option.answer))
                        {
                            fieldValue.value = option.answer;
                        }
                    }
                }
                ParticipantRepository.UpdateFields(field);
            }

            return QuestionRepository.UpdateQuestion<DemographicQuestion>(question);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public Boolean UpdateNumeric(NumericQuestion q)
        {
            var question = QuestionRepository.GetQuestionByID<NumericQuestion>(q.questionID);
            PopulateQuestion(question, q);
            question.greaterThan = q.greaterThan;
            question.lessThan = q.lessThan;

            return QuestionRepository.UpdateQuestion<NumericQuestion>(question);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public Boolean UpdateAlphanumeric(AlphanumericQuestion q)
        {
            var question = QuestionRepository.GetQuestionByID<AlphanumericQuestion>(q.questionID);
            PopulateQuestion(question, q);
            question.answers = q.answers;

            return QuestionRepository.UpdateQuestion<AlphanumericQuestion>(question);
        }

        private int AddOption<T>(int questionID, String optionText, int optionNumber) where T : MultipleChoiceQuestion
        {
            var question = QuestionRepository.GetQuestionByID<T>(questionID);
            var option = new QuestionOption(optionText, optionNumber);
            question.options.Add(option);
            QuestionRepository.UpdateQuestion<T>(question);

            return option.optionID;
        }

        [Authorize]
        public int AddCustomOption(int questionID, String optionText, int optionNumber)
        {
            return AddOption<CustomQuestion>(questionID, optionText, optionNumber);
        }

        [Authorize]
        public int AddRankingOption(int questionID, String optionText, int optionNumber)
        {
            return AddOption<RankingQuestion>(questionID, optionText, optionNumber);
        }

        [Authorize]
        public int AddDemographicOption(int questionID, String optionText, int optionNumber)
        {
            return AddOption<DemographicQuestion>(questionID, optionText, optionNumber);
        }

        [Authorize]
        public int AddAnswerString(int questionID, String answer)
        {
            var question = QuestionRepository.GetQuestionByID<AlphanumericQuestion>(questionID);
            var correctAnswer = new CorrectAnswer(answer);
            question.answers.Add(correctAnswer);
            QuestionRepository.UpdateQuestion<AlphanumericQuestion>(question);

            return correctAnswer.answerID;
        }

        private Boolean DeleteOption<T>(int questionID, int optionID, int optionNumber) where T : MultipleChoiceQuestion
        {
            var question = QuestionRepository.GetQuestionByID<T>(questionID);
            question.options.RemoveAt(optionNumber - 1);
            foreach (var option in question.options)
            {
                if (option.optionNumber > optionNumber)
                {
                    option.optionNumber--;
                }
            }
            QuestionRepository.UpdateQuestion<T>(question);

            return QuestionRepository.DeleteOptionByID(optionID);
        }

        [Authorize]
        public Boolean DeleteCustomOption(int questionID, int optionID, int optionNumber)
        {
            return DeleteOption<CustomQuestion>(questionID, optionID, optionNumber);
        }

        [Authorize]
        public Boolean DeleteRankingOption(int questionID, int optionID, int optionNumber)
        {
            RankingQuestion question = QuestionRepository.GetQuestionByID<RankingQuestion>(questionID);
            RankValue value = question.rankValues.ElementAt(question.rankValues.Count-1);
            return DeleteOption<RankingQuestion>(questionID, optionID, optionNumber);
        }

        [Authorize]
        public Boolean DeleteDemographicOption(int questionID, int optionID, int optionNumber)
        {
            return DeleteOption<DemographicQuestion>(questionID, optionID, optionNumber);
        }

        private Boolean SwapOptions<T>(int questionID, int optionNumber1, int optionNumber2) where T : MultipleChoiceQuestion
        {
            var question = QuestionRepository.GetQuestionByID<T>(questionID);
            var option1 = question.options[optionNumber1 - 1];
            var option2 = question.options[optionNumber2 - 1];
            option1.optionNumber = optionNumber2;
            option2.optionNumber = optionNumber1;
            question.options[optionNumber1 - 1] = option2;
            question.options[optionNumber2 - 1] = option1;

            return QuestionRepository.UpdateQuestion<T>(question);
        }

        [Authorize]
        public Boolean SwapCustomOptions(int questionID, int optionNumber1, int optionNumber2)
        {
            return SwapOptions<CustomQuestion>(questionID, optionNumber1, optionNumber2);
        }

        [Authorize]
        public Boolean SwapRankingOptions(int questionID, int optionNumber1, int optionNumber2)
        {
            return SwapOptions<RankingQuestion>(questionID, optionNumber1, optionNumber2);
        }

        [Authorize]
        public Boolean SwapDemographicOptions(int questionID, int optionNumber1, int optionNumber2)
        {
            return SwapOptions<DemographicQuestion>(questionID, optionNumber1, optionNumber2);
        }

        [Authorize]
        public Boolean ApplyTemplate(int questionID, String template)
        {
            var question = QuestionRepository.GetQuestionByID<CustomQuestion>(questionID);
            var optionsTemplate = TemplateRepository.GetOptionsTemplateByName(template);
            var options = question.options;
            question.options = new List<QuestionOption>();
            if (!QuestionRepository.DeleteOptions(options))
            {
                return false;
            }
            foreach (var option in optionsTemplate.options)
            {
                question.options.Add(new QuestionOption(option.text, option.optionNumber));
            }
            return QuestionRepository.UpdateQuestion<CustomQuestion>(question);
        }

        [Authorize]
        public ActionResult DeleteQuestion(int questionID, int pollID)
        {
            var poll = PollRepository.GetPollByID(pollID);
            var question = poll.questions.First(m => m.questionID == questionID);
            question.deletionTime = DateTime.Now;
            foreach (var q in poll.questions)
            {
                if (q.questionNumber > question.questionNumber && question.deletionTime == null)
                {
                    q.questionNumber--;
                }
            }
            PollRepository.UpdatePoll(poll);

            return RedirectToAction("Questions", "Poll", new { pollID = pollID });
        }

        [Authorize]
        public Boolean DeleteAnswerString(int questionID, int index)
        {
            var question = QuestionRepository.GetQuestionByID<AlphanumericQuestion>(questionID);
            var answer = question.answers[index];

            question.answers.RemoveAt(index);
            QuestionRepository.UpdateQuestion<AlphanumericQuestion>(question);

            return QuestionRepository.DeleteAnswerString(answer);
        }

        public int ConvertToDemographic(int questionID, int pollID)
        {
            var poll = PollRepository.GetPollByID(pollID);
            var question = QuestionRepository.GetQuestionByID<CustomQuestion>(questionID);
            var demographicQuestion = new DemographicQuestion(question.questionNumber);

            demographicQuestion.question = question.question;
            demographicQuestion.creationTime = question.creationTime;
            demographicQuestion.deletionTime = question.deletionTime;
            demographicQuestion.questionNumber = question.questionNumber;
            demographicQuestion.fontType = question.fontType;
            demographicQuestion.fontColour = question.fontColour;
            demographicQuestion.fontSize = question.fontSize;
            IList<Feedback> feedback = question.feedback.ToList();
            foreach (Feedback f in feedback) demographicQuestion.feedback.Add(f);
            demographicQuestion.allowedAnswers = question.allowedAnswers;
            demographicQuestion.resultsChart = question.resultsChart;
            demographicQuestion.comparisonChart = question.comparisonChart;
            demographicQuestion.options = question.options;
            IList<Participant> responded = question.responded.ToList();
            demographicQuestion.responded = responded;

            question.responded.Clear();
            question.feedback.Clear();
            question.resultsChart = null;
            question.comparisonChart = null;
            question.options = null;
            poll.questions.Insert(poll.questions.IndexOf(question) + 1, demographicQuestion);
            poll.questions.Remove(question);

            QuestionRepository.UpdateQuestion<CustomQuestion>(question);
            QuestionRepository.UpdateQuestion<DemographicQuestion>(demographicQuestion);
            PollRepository.UpdatePoll(poll);
            QuestionRepository.DeleteQuestionByID(question.questionID);

            return demographicQuestion.questionID;
        }

        public int ConvertToCustom(int questionID, int pollID)
        {
            var poll = PollRepository.GetPollByID(pollID);
            var question = QuestionRepository.GetQuestionByID<DemographicQuestion>(questionID);
            var customQuestion = new CustomQuestion(question.questionNumber);

            customQuestion.question = question.question;
            customQuestion.creationTime = question.creationTime;
            customQuestion.deletionTime = question.deletionTime;
            customQuestion.questionNumber = question.questionNumber;
            customQuestion.fontType = question.fontType;
            customQuestion.fontColour = question.fontColour;
            customQuestion.fontSize = question.fontSize;
            IList<Feedback> feedback = question.feedback.ToList();
            foreach (Feedback f in feedback) customQuestion.feedback.Add(f);
            customQuestion.allowedAnswers = question.allowedAnswers;
            customQuestion.resultsChart = question.resultsChart;
            customQuestion.comparisonChart = question.comparisonChart;
            customQuestion.options = question.options;
            IList<Participant> responded = question.responded.ToList();
            customQuestion.responded = responded;

            question.responded.Clear();
            question.feedback.Clear();
            question.resultsChart=null;
            question.comparisonChart=null;
            question.options=null;
            question.linkedField = null;

            poll.questions.Insert(poll.questions.IndexOf(question) + 1, customQuestion);
            poll.questions.Remove(question);
            QuestionRepository.UpdateQuestion<DemographicQuestion>(question);
            QuestionRepository.UpdateQuestion<CustomQuestion>(customQuestion);
            PollRepository.UpdatePoll(poll);
            QuestionRepository.DeleteQuestionByID(question.questionID);

            return customQuestion.questionID;
        }

        [Authorize(Roles = "Poll Master")]
        public JsonResult GetRespondedDeviceIDs(int questionID)
        {
            var responses = QuestionRepository.GetRespondedDeviceIDs(questionID);
            return Json(responses);
        }

        [Authorize]
        public ActionResult SelectCompareWith(int questionID)
        {
            var questionsWithPolls = QuestionRepository.GetQuestionsComparableWith(questionID);
            var question = QuestionRepository.GetQuestionByID<Question>(questionID);
            var poll = PollRepository.GetPollContainingQuestion(question);
            var questionAndPoll = new QuestionAndPoll(question, poll);
            var model = new SelectCompareWithViewModel(questionAndPoll, questionsWithPolls);
            return View(model);
        }

        public bool ClearCompareWith(int questionID, QuestionType type)
        {
            switch(type)
            {
                case QuestionType.Custom:
                    var question1 = QuestionRepository.GetQuestionByID<CustomQuestion>(questionID);
                    question1.compareWith = null;
                    return QuestionRepository.UpdateQuestion<CustomQuestion>(question1);

                case QuestionType.Demographic:
                    var question2 = QuestionRepository.GetQuestionByID<DemographicQuestion>(questionID);
                    question2.compareWith = null;
                    return QuestionRepository.UpdateQuestion<DemographicQuestion>(question2);

                case QuestionType.Ranking:
                    var question3 = QuestionRepository.GetQuestionByID<RankingQuestion>(questionID);
                    question3.compareWith = null;
                    return QuestionRepository.UpdateQuestion<RankingQuestion>(question3);

                default:
                    return false;
            }
        }

        public bool SelectComparitiveQuestion(int questionID, int comparisonQuestionID, QuestionType type)
        {
            switch(type)
            {
                case QuestionType.Custom:
                    var question1 = QuestionRepository.GetQuestionByID<CustomQuestion>(questionID);
                    var comparisonQuestion1 = QuestionRepository.GetQuestionByID<CustomQuestion>(comparisonQuestionID);
                    question1.compareWith = comparisonQuestion1;
                    return QuestionRepository.UpdateQuestion<CustomQuestion>(question1);

                case QuestionType.Demographic:
                    var question2 = QuestionRepository.GetQuestionByID<DemographicQuestion>(questionID);
                    var comparisonQuestion2 = QuestionRepository.GetQuestionByID<DemographicQuestion>(comparisonQuestionID);
                    question2.compareWith = comparisonQuestion2;
                    return QuestionRepository.UpdateQuestion<DemographicQuestion>(question2);

                case QuestionType.Ranking:
                    var question3 = QuestionRepository.GetQuestionByID<RankingQuestion>(questionID);
                    var comparisonQuestion3 = QuestionRepository.GetQuestionByID<RankingQuestion>(comparisonQuestionID);
                    question3.compareWith = comparisonQuestion3;
                    return QuestionRepository.UpdateQuestion<RankingQuestion>(question3);    

                default:
                    return false;
            }
        }
    }
}
